<?php
/*
 * AmazonAsocUtil FOR PHP
 * @author amigo*k.k http://clubamigokk.blogspot.jp/
 * @copyright Copyright © 2013 the App*Rocks All rights reserved.
 *
 */
class AmazonAsocUtil{
    /*
     * locales array
     */
    public $locales;
    /*
     * AmazonAsocUtil
     * @author amigo*kk
     * @retuen void
     */
    public function AmazonAsocUtil(){

        $this->locales= Array();
        $this->locales['JP']='JP';
        $this->locales['US']='US';
        $this->locales['CA']='CA';
        $this->locales['FR']='FR';
        $this->locales['UK']='UK';
        $this->locales['DE']='DE';
        $this->locales['CN']='CN';
        $this->locales['IT']='IT';
        $this->locales['ES']='ES';

    }
    /*
     * getSearchIndexHtml
     * @param  Array $searchIndexOptions
     * @param  String $selectedIndex
     * @return String html element. select and option tag
     * @author amigo*k.k
     */
    public function getSearchIndexHtml($searchIndexOptions, $selectedIndex) {

        $searchIndexOptions["Apparel"] = "Apparel";
        $searchIndexOptions["Automotive"] = "Automotive";
        $searchIndexOptions["Books"] = "Books";
        $searchIndexOptions["DVD"] = "DVD";
        $searchIndexOptions["Electronics"] = "Electronics";
        $searchIndexOptions["GourmetFood"] = "GourmetFood";
        $searchIndexOptions["Kitchen"] = "Kitchen";
        $searchIndexOptions["Music"] = "Music";
        $searchIndexOptions["PCHardware"] = "PCHardware";
        $searchIndexOptions["PetSupplies"] = "PetSupplies";
        $searchIndexOptions["Software"] = "Software";
        $searchIndexOptions["SoftwareVideoGames"] = "SoftwareVideoGames";
        $searchIndexOptions["SportingGoods"] = "SportingGoods";
        $searchIndexOptions["Tools"] = "Tools";
        $searchIndexOptions["Toys"] = "Toys";
        $searchIndexOptions["VHS"] = "VHS";
        $searchIndexOptions["VideoGames"] = "VideoGames";
        $optiontag = "";
        foreach ($searchIndexOptions as $key => $value) {
            $optiontag .= "<option value='" . $key . "'";
            if ($key == $selectedIndex) {
                $optiontag .= " selected";
            }
            $optiontag .=" >" . $value . "</option>" ;
        }
        $html_element = "<label for='search_index' class='select'>SearchIndex:</label><select id='search_index' name='search_index' data-mini='true' >" . $optiontag . "</select>";
        return $html_element;
    }
    /*
     * itemsearch
     * @param $amazon object BY PEAR
     * @param $options searchIndexOptions
     * @param searchIndex
     * @author amigo*k.k
     */
    function itemsearch($amazon, $options, $searchIndex,$locale) {
        $response = "";
        $url= 'http://ecs.amazonaws.jp/onca/xml';
        //echo "<br/>srch locale:".$locale.  time();
        switch (strtoupper($locale)) {
            case "CA":
                $url = "http://webservices.amazon.ca/onca/xml";
                break;
            case "CN":
                $url = "http://webservices.amazon.cn/onca/xml";
                break;
            case "DE":
                $url = "http://webservices.amazon.de/onca/xml";
                break;
            case "ES":
                $url = "http://webservices.amazon.es/onca/xml";
                break;
            case "FR":
                $url = "http://webservices.amazon.fr/onca/xml";
                break;
            case "IT":
                $url = "http://webservices.amazon.it/onca/xml";
                break;
            case "JP":
                $url = "http://webservices.amazon.co.jp/onca/xml";
                break;
            case "UK":
                $url = "http://webservices.amazon.co.uk/onca/xml";
                break;
            case "US":
                $url = "http://webservices.amazon.com/onca/xml";
                break;
            default:
                break;
        }

        $amazon->setBaseUrl($url);
        $response = $amazon->ItemSearch($searchIndex, $options);
        return $response;
    }
    /*
     * createLocaleLink
     * @param $default_locale locale String
     * @retuen hyperlink
     * @author amigo*k.k
     */
    public function createLocaleLink($default_locale){
        $html="<ul id='countries'>";
        //foreach ($this->locales as $value) {
        if(!is_array($this->locales)){
            $this->locales=$this->AmazonAsocUtil();
        }
        foreach ($this->locales as $key => $value){
                if($value==strtoupper($default_locale)){
                      $html .= "<li><p> ".strtolower($key)."</p></li>";
                }else{
                      $html .= "<li><a data-ajax='false'  href='/" . strtolower($value) . "/index.php?locale=" .strtolower($key). "'" . ">" . $value . "</a></li>";
                }
        }
        $html.="</ul>";
        return $html;
    }
}
?>
